import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/vpn_provider.dart';
import '../services/ip_location_service.dart';
import '../widgets/collapsible_card.dart';
import '../utils/theme.dart';
import '../l10n/app_localizations.dart';

class IpLocationCard extends StatelessWidget {
  const IpLocationCard({super.key});

  @override
  Widget build(BuildContext context) {
    final l10n = AppLocalizations.of(context);
    
    return Consumer<VpnProvider>(
      builder: (context, vpnProvider, _) {
        return CollapsibleCard(
          title: l10n.translate('info_location'),
          icon: Icons.public,
          color: AppTheme.primaryColor,
          initiallyExpanded: true,
          onRefresh: () async {
            // TODO: 刷新IP位置信息
          },
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 本机IP信息
              _buildIpLocationRow(
                icon: Icons.home_outlined,
                title: l10n.locationOriginalLocation,
                flag: vpnProvider.originalCountryFlag,
                location: vpnProvider.originalLocation,
                isLoading: vpnProvider.isLoadingOriginalLocation,
                l10n: l10n,
                color: Colors.blue,
              ),
                
                // 分隔线（仅在连接时显示）
                if (vpnProvider.status == VpnStatus.connected) ...[
                  const SizedBox(height: 16),
                  Row(
                    children: [
                      Expanded(child: Divider(color: Colors.grey.shade300)),
                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 12),
                        child: Icon(
                          Icons.arrow_downward,
                          size: 16,
                          color: Colors.grey.shade400,
                        ),
                      ),
                      Expanded(child: Divider(color: Colors.grey.shade300)),
                    ],
                  ),
                  const SizedBox(height: 16),
                  
                  // 代理IP信息
                  _buildIpLocationRow(
                    icon: Icons.vpn_lock_outlined,
                    title: l10n.locationProxyLocation,
                    flag: vpnProvider.proxyCountryFlag,
                    location: vpnProvider.proxyLocation,
                    isLoading: vpnProvider.isLoadingProxyLocation,
                    l10n: l10n,                  color: AppTheme.primaryColor,
                ),
              ],
            ],
          ),
        );
      },
    );
  }

  Widget _buildIpLocationRow({
    required IconData icon,
    required String title,
    required String flag,
    required IpLocationInfo? location,
    required bool isLoading,
    required AppLocalizations l10n,
    required Color color,
  }) {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: color.withOpacity(0.05),
        borderRadius: BorderRadius.circular(12),
        border: Border.all(
          color: color.withOpacity(0.2),
          width: 1.5,
        ),
      ),
      child: Row(
        children: [
          // 图标
          Container(
            padding: const EdgeInsets.all(10),
            decoration: BoxDecoration(
              color: color.withOpacity(0.15),
              borderRadius: BorderRadius.circular(10),
            ),
            child: Icon(icon, color: color, size: 24),
          ),
          const SizedBox(width: 16),
          
          // 信息内容
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: TextStyle(
                    fontSize: 12,
                    color: Colors.grey.shade600,
                    fontWeight: FontWeight.w600,
                    letterSpacing: 0.5,
                  ),
                ),
                const SizedBox(height: 8),
                
                if (isLoading)
                  Row(
                    children: [
                      SizedBox(
                        width: 14,
                        height: 14,
                        child: CircularProgressIndicator(
                          strokeWidth: 2,
                          valueColor: AlwaysStoppedAnimation<Color>(color),
                        ),
                      ),
                      const SizedBox(width: 10),
                      Text(
                        l10n.locationLoading,
                        style: TextStyle(
                          fontSize: 13,
                          color: Colors.grey.shade500,
                          fontStyle: FontStyle.italic,
                        ),
                      ),
                    ],
                  )
                else if (location != null)
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      // 国旗和位置
                      Row(
                        children: [
                          Text(
                            flag,
                            style: const TextStyle(fontSize: 24),
                          ),
                          const SizedBox(width: 10),
                          Expanded(
                            child: Text(
                              location.shortLocation,
                              style: const TextStyle(
                                fontSize: 16,
                                fontWeight: FontWeight.bold,
                                color: Colors.black87,
                              ),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(height: 6),
                      
                      // IP地址
                      Row(
                        children: [
                          Icon(
                            Icons.language,
                            size: 14,
                            color: Colors.grey.shade600,
                          ),
                          const SizedBox(width: 6),
                          Expanded(
                            child: Text(
                              location.query,
                              style: TextStyle(
                                fontSize: 13,
                                color: Colors.grey.shade700,
                                fontFamily: 'monospace',
                                letterSpacing: 0.5,
                              ),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                        ],
                      ),
                      
                      // ISP信息（如果有）
                      if (location.isp.isNotEmpty) ...[
                        const SizedBox(height: 4),
                        Row(
                          children: [
                            Icon(
                              Icons.business,
                              size: 12,
                              color: Colors.grey.shade500,
                            ),
                            const SizedBox(width: 6),
                            Expanded(
                              child: Text(
                                location.isp,
                                style: TextStyle(
                                  fontSize: 11,
                                  color: Colors.grey.shade500,
                                ),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                          ],
                        ),
                      ],
                    ],
                  )
                else
                  Row(
                    children: [
                      Icon(
                        Icons.help_outline,
                        size: 16,
                        color: Colors.grey.shade400,
                      ),
                      const SizedBox(width: 8),
                      Text(
                        l10n.locationUnknown,
                        style: TextStyle(
                          fontSize: 13,
                          color: Colors.grey.shade500,
                        ),
                      ),
                    ],
                  ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
